package com.imooc.business.builder;

import com.imooc.infra.repository.jdbc.params.StandardSqlParam;
import com.imooc.module.collect.enums.DimensionEnum;

import java.util.List;
import java.util.Map;

/**
 * @Author Tim
 * @Description: 存储sql构建器
 * @Version 1.0
 */
public class StorageSqlBuilder {

    /**
     * 构建删除指标数据sql
     * @param tableName
     * @param dimensionEnum
     * @return
     */
    public static String buildDeleteSql(String tableName, DimensionEnum dimensionEnum) {
        return switch (dimensionEnum) {
            case USER -> "delete from "+ tableName + " where "+ StandardSqlParam.COUNT_TIME + " = :" +StandardSqlParam.COUNT_TIME +" and user_id in (:user_ids) and group_id = :group_id; ";
            case GROUP -> "delete from "+ tableName + " where "+ StandardSqlParam.COUNT_TIME + " = :" +StandardSqlParam.COUNT_TIME +" and group_id = :group_id and school_id = :school_id; ";
            case SCHOLL -> "delete from "+ tableName + " where "+ StandardSqlParam.COUNT_TIME + " = :" +StandardSqlParam.COUNT_TIME +" and school_id = :school_id; ";
        };
    }

    /**
     * 构建存储sql
     * @param tableName
     * @param results
     * @return
     */
    public static String buildInsertSql(String tableName, List<Map<String, Object>> results) {
        String insertSql = "insert into "+ tableName +
                "("+buildColumn(results, ",")+")"+
                " values "+
                "(:"+buildColumn(results, ",:")+");\n";
        return insertSql;
    }
    //insert into user(name,age,id) values(name,:age,:id);

    private static String buildColumn(List<Map<String, Object>> rows, String delimiter) {
        return String.join(delimiter, rows.get(0).keySet());
    }
}
